package com.rfs.suanfa;

import java.util.HashMap;

/**
 * @author renfushuai
 * @date 2022/7/25
 */
public class 最长回文子串 {
    public static void main(String[] args) {
        String s = longestPalindrome("babad");
        System.out.println(s);
    }
    public static String longestPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        if (s.length() == 1) {
            return s;
        }
        char[] chars = s.toCharArray();
        HashMap<String,Integer> map = new HashMap<>();
        String longest = "";
        for (int i = 0; i < chars.length; i++) {
            String key = String.valueOf(chars[i]) + i;
            map.put(key,i);
            if (map.containsKey(key)) {
                if (longest.length()<s.substring(map.get(key), i+1).length()){
                    longest = s.substring(map.get(key), i+1);
                }
            }

        }
        return longest;
    }
}
